package com.by.yz.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.toolbox.NetworkImageView;
import com.bumptech.glide.Glide;
import com.by.yz.App;
import com.by.yz.R;
import com.by.yz.activity.AddFriendActivity;
import com.by.yz.activity.CurrentTopicsActivity;
import com.by.yz.custom.CircleNetworkImageView;
import com.by.yz.entity.Account;
import com.by.yz.entity.Click;
import com.by.yz.entity.TopicInfo;
import com.by.yz.entity.UserTest;
import com.by.yz.loginUtil.AccountRESTFul;
import com.by.yz.loginUtil.ClickRESTtful;
import com.by.yz.loginUtil.TopicInfoRESTFul;
import com.by.yz.loginUtil.UserRESTFul;
import com.by.yz.util.Start_End_times;
import com.by.yz.util.ToJSonObject;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by mrs yang on 2016/10/26.
 */
public class TopicInfoAdapter extends RecyclerView.Adapter<TopicInfoAdapter.ViewHolder> {
    private final TypedValue mTypedValue = new TypedValue();
    private int mBackground;
    private List<TopicInfo> mValues;
    private Start_End_times set;
    private UserTest userTest;
    private Account account;
    Click click;
    Integer Dcount;//点赞数
    Context con;


    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TopicInfo tt;
        public final View mView;
        public final NetworkImageView topicimage;
        public final TextView hot_topic_title;
        public final TextView topicContent;
        public final TextView report_time;
        public final CircleNetworkImageView headimg;
        public final TextView reporter;
        public final TextView dianzan_count;
        public final TextView comment_count;
        public final LinearLayout comment;
        public final LinearLayout dianzan;
        final ImageView dianzan_image;


        public ViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
            topicimage = (NetworkImageView) itemView.findViewById(R.id.healthtopic);
            hot_topic_title = (TextView) itemView.findViewById(R.id.health_topic_title);
            topicContent=(TextView) itemView.findViewById(R.id.health_topic_content);
            report_time = (TextView) itemView.findViewById(R.id.report_time);
            headimg=(CircleNetworkImageView) itemView.findViewById(R.id.reporter_image);
            reporter = (TextView) itemView.findViewById(R.id.reporter);
            dianzan_count =(TextView) itemView.findViewById(R.id.dianzan_count);
            comment_count =(TextView) itemView.findViewById(R.id.comment_count);
            comment=(LinearLayout)itemView.findViewById(R.id.comment);
            dianzan=(LinearLayout)itemView.findViewById(R.id.dianzan);
            dianzan_image=(ImageView)itemView.findViewById(R.id.dianzan_image);
        }

    }

    public TopicInfo getValueAt(int position) {
        return mValues.get(position);
    }

    public TopicInfoAdapter(Context context, List<TopicInfo> items) {
        context.getTheme().resolveAttribute(R.attr.selectableItemBackground, mTypedValue, true);
        mBackground = mTypedValue.resourceId;
        mValues = items;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.health_topic_item, parent, false);
        view.setBackgroundResource(mBackground);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder,final int position) {
        holder.tt = mValues.get(position);
        String topicimagepath=mValues.get(position).getTopicimage();
        String[] imagepath=topicimagepath.split(",");
        holder.topicimage.setDefaultImageResId(R.mipmap.health_passage_default);
        holder.topicimage.setErrorImageResId(R.mipmap.health_topic_error);
        if (!topicimagepath.equals("") && imagepath.length!=0) {
            holder.topicimage.setImageUrl(imagepath[0].toString(), App.imageLoader);
//            Glide.with(holder.topicimage.getContext())
//            .load(imagepath[0].toString())
//            .fitCenter()
//            .into(holder.topicimage);
        }
        final TopicInfo bean=mValues.get(position);
        String time=bean.getCreateat();
        String time1=time.replace("T"," ");
        String time2=time1.substring(0,19);
        set=new Start_End_times(time2);
        holder.report_time.setText(set.str_date());
        AccountRESTFul accountRESTFul=new AccountRESTFul();
        accountRESTFul.get(new AccountRESTFul.IhttpCb() {
            @Override
            public void ok(JSONObject response) {
                JSONObject jsonObject=response;
                account=ToJSonObject.jsonToBean(jsonObject.toString(),Account.class);
                ClickRESTtful clickRESTtful = new ClickRESTtful();
                clickRESTtful.getTopic(mValues.get(position).getId(), new ClickRESTtful.MyGetCallBack() {
                    @Override
                    public void ok(JSONArray response) throws JSONException {
                        if (response.length()==0){
                            holder.dianzan_image.setImageResource(R.mipmap.community_sign_up_normal);
                        }else {
                            for (int i = 0; i < response.length(); i++) {
                                click = new Click();
                                JSONObject jsonObject = response.getJSONObject(i);
                                click = ToJSonObject.jsonToBean(jsonObject.toString(), Click.class);
                                if (click.getTopic().equals(mValues.get(position).getId()) && click.getClicker().equals(account.getLogin())) {
                                    holder.dianzan_image.setImageResource(R.mipmap.community_sign_up_press);
                                    holder.dianzan_image.setClickable(false);
                                }
                            }
                        }
                    }
                    @Override
                    public void error(String error) {
                        holder.dianzan_image.setImageResource(R.mipmap.community_sign_up_normal);
                    }
                });
            }
            @Override
            public void error(String error) {
                Log.e("account-error","授权失败"+error);
            }
        });
        holder.headimg.setDefaultImageResId(R.mipmap.headimg);
        holder.headimg.setErrorImageResId(R.mipmap.headimg_error);
        UserRESTFul userRESTFul=new UserRESTFul();
        userRESTFul.getUser(mValues.get(position).getCreater(), new UserRESTFul.UserPOSTCallBack() {
            @Override
            public void ok(JSONObject response) throws JSONException {
                userTest = ToJSonObject.jsonToBean(response.toString(), UserTest.class);
                holder.reporter.setText(userTest.getnickName());
                if (userTest.getHeadimg()!=null){
//                    Glide.with(holder.headimg.getContext())
//                            .load(userTest.getHeadimg())
//                            .fitCenter()
//                            .into(holder.headimg);
                    holder.headimg.setImageUrl(userTest.getHeadimg(), App.imageLoader);
                }

                Log.e("ylj", "user" + userTest.toString());
            }

            @Override
            public void error(String response) {

            }
        });
        holder.hot_topic_title.setText(bean.getTitle());
        holder.topicContent.setText(bean.getContent());
        Integer dcount=mValues.get(position).getCount();
        Integer ccount=mValues.get(position).getDisscusscount();
        holder.dianzan_count.setText(dcount.toString());
        holder.comment_count.setText(ccount.toString());

        holder.headimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Context context = view.getContext();
                Intent intent = new Intent(context, AddFriendActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("currentopics",bean.getCreater());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
        holder.dianzan_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                con = v.getContext();
                click = new Click();
                click.setClicker(account.getLogin());
                click.setIsclick(true);
                click.setTopic(mValues.get(position).getId());
                JSONObject jsonObject = ToJSonObject.beanToJSONObejct(click);
                ClickRESTtful clickRESTtful = new ClickRESTtful();
                clickRESTtful.postClick(jsonObject, new ClickRESTtful.MyPostCallBack() {
                    @Override
                    public void ok(JSONObject response) throws JSONException {
                        Log.e("click-response", response.toString());
                        Dcount = holder.tt.getCount()+1;
                        Log.e("Dcount = ",Dcount+"");
                        holder.dianzan_image.setImageResource(R.mipmap.community_sign_up_press);
                        holder.dianzan_count.setText(Dcount+"");
                        holder.tt.setCount(Dcount);
                        JSONObject jsonObject = ToJSonObject.beanToJSONObejct(holder.tt);
                        TopicInfoRESTFul topicful = new TopicInfoRESTFul();
                        topicful.putTopicInfo(jsonObject, new TopicInfoRESTFul.MyPostCallBack() {
                            @Override
                            public void ok(JSONObject response) throws JSONException {
                                Log.e("topInfo-response", response.toString());
                                holder.dianzan.setClickable(false);
                                Toast.makeText(con, "点赞成功！", Toast.LENGTH_SHORT).show();
                                Log.e("成功", "点赞成功");
                            }
                            @Override
                            public void error(String error) {
                                Log.e("失败", "点赞失败" + error);
                            }
                        });
                    }
                    @Override
                    public void error(String error) {
                        Log.e("eeeee", "点赞失败" + error);
                    }
                });
            }
        });
        holder.comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                Intent intent = new Intent(context, CurrentTopicsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("currentopic",holder.tt);
                intent.putExtras(bundle);
                intent.putExtra("isComment",true);
                context.startActivity(intent);
            }
        });
        holder.mView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Context context = v.getContext();
                Intent intent = new Intent(context, CurrentTopicsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("currentopic",holder.tt);
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mValues.size();
    }
}
